//
//  CheckBoxView.m
//  ScanCode
//
//  Created by YiXuePxh on 15/9/17.
//  Copyright (c) 2015年 上海益学. All rights reserved.
//

#import "CheckBoxView.h"
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
#define RatioWidth ScreenWidth / 320.0
#define RatioHeight ScreenHeight / 480.0
@interface CheckBoxView()
{
    int _num;
    BOOL _upOrDown;
    NSTimer* _timer;
}
@property(nonatomic,strong)UIImageView* line;
@end
@implementation CheckBoxView
-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        //初始化相关参数
        _num = 0;
        _upOrDown = YES;
        
        UIImageView* checkBox = [[UIImageView alloc]initWithFrame:CGRectMake(10 * RatioWidth, 100 * RatioHeight, 300 * RatioWidth, 300 * RatioHeight)];
        [self addSubview:checkBox];
        [checkBox setImage:[UIImage imageNamed:@"pick_bg"]];
        _transparentArea = checkBox.frame.size;
        
        _line = [[UIImageView alloc]initWithFrame:CGRectMake(50 * RatioWidth, 110 * RatioHeight, 220 * RatioWidth, 2 * RatioHeight)];
        [self addSubview:_line];
        [_line setImage:[UIImage imageNamed:@"line"]];
        
        UILabel* introduction = [[UILabel alloc]initWithFrame:CGRectMake(25 * RatioWidth, 360 * RatioHeight, 270 * RatioWidth, 25 * RatioHeight)];
        [self addSubview:introduction];
        [introduction setFont:[UIFont systemFontOfSize:16.0]];
        [introduction setTextAlignment:NSTextAlignmentCenter];
        [introduction setBackgroundColor:[UIColor clearColor]];
        introduction.text = @"对准二维码/条形码，即可自动扫描";
        
        _timer = [NSTimer scheduledTimerWithTimeInterval:.02 target:self selector:@selector(timeAction) userInfo:nil repeats:YES];
        self.backgroundColor = [UIColor clearColor];//清除背景上的所有颜色
    }
    return self;
}
-(void)timeAction{
    if (_upOrDown) {
        _num ++;
        _line.frame = CGRectMake(50 * RatioWidth, 110 * RatioHeight + 2 * _num, 220 * RatioWidth, 2 * RatioHeight);
        if (2 * _num == 280) {
            _upOrDown = NO;
        }
    }else{
        _num --;
        _line.frame = CGRectMake(50 * RatioWidth, 110 * RatioHeight + 2 * _num, 220 * RatioWidth, 2 * RatioHeight);
        if (_num == 0) {
            _upOrDown = YES;
        }
    }
}
-(void)drawRect:(CGRect)rect{
//    整个二维码扫描界面的颜色
    CGRect screenDrawRect = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
    //中间清空的矩形框
    CGRect clearDrawRect = CGRectMake(ScreenWidth * 0.5 - _transparentArea.width * 0.5, ScreenHeight * 0.5 - _transparentArea.height * 0.5, _transparentArea.width, _transparentArea.height);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [self addScreenFillRect:ctx rect:screenDrawRect];
    [self addCenterClearRect:ctx rect:clearDrawRect];
    [self addWhiteRect:ctx rect:clearDrawRect];
    
    [self addCornerLineWithContext:ctx rect:clearDrawRect];
}
//跟背景一个灰色
-(void)addScreenFillRect:(CGContextRef)ctx rect:(CGRect)rect{
    CGContextSetRGBFillColor(ctx, 40 / 255.0, 40 / 255.0, 40 / 255.0, 0.5);
    CGContextFillRect(ctx, rect);//draw the transparent layer
}
//在页面中切出一个透明的区域
-(void)addCenterClearRect:(CGContextRef)ctx rect:(CGRect)rect{
    CGContextClearRect(ctx, rect);//clear the center rect of the layer
}
-(void)addWhiteRect:(CGContextRef)ctx rect:(CGRect)rect{
    CGContextStrokeRect(ctx, rect);
    CGContextSetRGBStrokeColor(ctx, 1, 1, 1, 1);
    CGContextSetLineWidth(ctx, 0.8);
    CGContextAddRect(ctx, rect);
    CGContextStrokePath(ctx);
}
-(void)addCornerLineWithContext:(CGContextRef)ctx rect:(CGRect)rect{
    //画四个圆角
    CGContextSetLineWidth(ctx, 2);
    CGContextSetRGBStrokeColor(ctx, 83 / 255.0, 239 / 255.0, 111 / 255.0, 1);//绿色
    //左上角
    CGPoint poinsTopLeftA[] = {
        CGPointMake(rect.origin.x + 0.7, rect.origin.y),
        CGPointMake(rect.origin.x + 0.7, rect.origin.y + 15)
    };
    CGPoint poinsTopLeftB[] = {
        CGPointMake(rect.origin.x, rect.origin.y + 0.7),
        CGPointMake(rect.origin.x + 15, rect.origin.y + 0.7)
    };
    [self addLine:poinsTopLeftA pointB:poinsTopLeftB ctx:ctx];
    //左下角
    CGPoint poinsBottomLeftA[] = {
        CGPointMake(rect.origin.x+ 0.7, rect.origin.y + rect.size.height - 15),
        CGPointMake(rect.origin.x +0.7,rect.origin.y + rect.size.height)
    };
    CGPoint poinsBottomLeftB[] = {
        CGPointMake(rect.origin.x , rect.origin.y + rect.size.height - 0.7) ,
        CGPointMake(rect.origin.x+0.7 +15, rect.origin.y + rect.size.height - 0.7)
    };
    [self addLine:poinsBottomLeftA pointB:poinsBottomLeftB ctx:ctx];
    //右上角
    CGPoint poinsTopRightA[] = {
        CGPointMake(rect.origin.x + rect.size.width - 15, rect.origin.y + 0.7),
        CGPointMake(rect.origin.x + rect.size.width, rect.origin.y + 0.7)
    };
    CGPoint poinsTopRightB[] = {
        CGPointMake(rect.origin.x + rect.size.width - 0.7, rect.origin.y),
        CGPointMake(rect.origin.x + rect.size.width - 0.7, rect.origin.y + 15 + 0.7)
    };
    [self addLine:poinsTopRightA pointB:poinsTopRightB ctx:ctx];
    //右下角
    CGPoint poinsBottomRightA[] = {
        CGPointMake(rect.origin.x + rect.size.width - 0.7, rect.origin.y + rect.size.height - 15),
        CGPointMake(rect.origin.x - 0.7 + rect.size.width, rect.origin.y + rect.size.height)
    };
    CGPoint poinsBottomRightB[] = {
        CGPointMake(rect.origin.x+ rect.size.width - 15 , rect.origin.y + rect.size.height-0.7),
        CGPointMake(rect.origin.x + rect.size.width,rect.origin.y + rect.size.height - 0.7 )
    };
    [self addLine:poinsBottomRightA pointB:poinsBottomRightB ctx:ctx];
    
    CGContextStrokePath(ctx);
}
-(void)addLine:(CGPoint[])pointA pointB:(CGPoint[])pointB ctx:(CGContextRef)ctx{
    CGContextAddLines(ctx, pointA, 2);
    CGContextAddLines(ctx, pointB, 2);
}
-(void)stopRefresh{
    [_line setHidden:YES];
    [_timer invalidate];
    _timer = nil;
}
@end
